{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>duration</th>\n",
       "      <th>src_bytes</th>\n",
       "      <th>dst_bytes</th>\n",
       "      <th>land</th>\n",
       "      <th>wrong_fragment</th>\n",
       "      <th>urgent</th>\n",
       "      <th>hot</th>\n",
       "      <th>num_failed_logins</th>\n",
       "      <th>logged_in</th>\n",
       "      <th>num_compromised</th>\n",
       "      <th>...</th>\n",
       "      <th>dst_host_count</th>\n",
       "      <th>dst_host_srv_count</th>\n",
       "      <th>dst_host_same_srv_rate</th>\n",
       "      <th>dst_host_diff_srv_rate</th>\n",
       "      <th>dst_host_same_src_port_rate</th>\n",
       "      <th>dst_host_srv_diff_host_rate</th>\n",
       "      <th>dst_host_serror_rate</th>\n",
       "      <th>dst_host_srv_serror_rate</th>\n",
       "      <th>dst_host_rerror_rate</th>\n",
       "      <th>dst_host_srv_rerror_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>494021.000000</td>\n",
       "      <td>4.940210e+05</td>\n",
       "      <td>4.940210e+05</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "      <td>494021.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>47.979302</td>\n",
       "      <td>3.025610e+03</td>\n",
       "      <td>8.685324e+02</td>\n",
       "      <td>0.000045</td>\n",
       "      <td>0.006433</td>\n",
       "      <td>0.000014</td>\n",
       "      <td>0.034519</td>\n",
       "      <td>0.000152</td>\n",
       "      <td>0.148247</td>\n",
       "      <td>0.010212</td>\n",
       "      <td>...</td>\n",
       "      <td>232.470778</td>\n",
       "      <td>188.665670</td>\n",
       "      <td>0.753780</td>\n",
       "      <td>0.030906</td>\n",
       "      <td>0.601935</td>\n",
       "      <td>0.006684</td>\n",
       "      <td>0.176754</td>\n",
       "      <td>0.176443</td>\n",
       "      <td>0.058118</td>\n",
       "      <td>0.057412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>707.746472</td>\n",
       "      <td>9.882181e+05</td>\n",
       "      <td>3.304000e+04</td>\n",
       "      <td>0.006673</td>\n",
       "      <td>0.134805</td>\n",
       "      <td>0.005510</td>\n",
       "      <td>0.782103</td>\n",
       "      <td>0.015520</td>\n",
       "      <td>0.355345</td>\n",
       "      <td>1.798326</td>\n",
       "      <td>...</td>\n",
       "      <td>64.745380</td>\n",
       "      <td>106.040437</td>\n",
       "      <td>0.410781</td>\n",
       "      <td>0.109259</td>\n",
       "      <td>0.481309</td>\n",
       "      <td>0.042133</td>\n",
       "      <td>0.380593</td>\n",
       "      <td>0.380919</td>\n",
       "      <td>0.230590</td>\n",
       "      <td>0.230140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>4.500000e+01</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>255.000000</td>\n",
       "      <td>46.000000</td>\n",
       "      <td>0.410000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>5.200000e+02</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>255.000000</td>\n",
       "      <td>255.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.032000e+03</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>255.000000</td>\n",
       "      <td>255.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.040000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>58329.000000</td>\n",
       "      <td>6.933756e+08</td>\n",
       "      <td>5.155468e+06</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>884.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>255.000000</td>\n",
       "      <td>255.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            duration     src_bytes     dst_bytes           land  \\\n",
       "count  494021.000000  4.940210e+05  4.940210e+05  494021.000000   \n",
       "mean       47.979302  3.025610e+03  8.685324e+02       0.000045   \n",
       "std       707.746472  9.882181e+05  3.304000e+04       0.006673   \n",
       "min         0.000000  0.000000e+00  0.000000e+00       0.000000   \n",
       "25%         0.000000  4.500000e+01  0.000000e+00       0.000000   \n",
       "50%         0.000000  5.200000e+02  0.000000e+00       0.000000   \n",
       "75%         0.000000  1.032000e+03  0.000000e+00       0.000000   \n",
       "max     58329.000000  6.933756e+08  5.155468e+06       1.000000   \n",
       "\n",
       "       wrong_fragment         urgent            hot  num_failed_logins  \\\n",
       "count   494021.000000  494021.000000  494021.000000      494021.000000   \n",
       "mean         0.006433       0.000014       0.034519           0.000152   \n",
       "std          0.134805       0.005510       0.782103           0.015520   \n",
       "min          0.000000       0.000000       0.000000           0.000000   \n",
       "25%          0.000000       0.000000       0.000000           0.000000   \n",
       "50%          0.000000       0.000000       0.000000           0.000000   \n",
       "75%          0.000000       0.000000       0.000000           0.000000   \n",
       "max          3.000000       3.000000      30.000000           5.000000   \n",
       "\n",
       "           logged_in  num_compromised            ...             \\\n",
       "count  494021.000000    494021.000000            ...              \n",
       "mean        0.148247         0.010212            ...              \n",
       "std         0.355345         1.798326            ...              \n",
       "min         0.000000         0.000000            ...              \n",
       "25%         0.000000         0.000000            ...              \n",
       "50%         0.000000         0.000000            ...              \n",
       "75%         0.000000         0.000000            ...              \n",
       "max         1.000000       884.000000            ...              \n",
       "\n",
       "       dst_host_count  dst_host_srv_count  dst_host_same_srv_rate  \\\n",
       "count   494021.000000       494021.000000           494021.000000   \n",
       "mean       232.470778          188.665670                0.753780   \n",
       "std         64.745380          106.040437                0.410781   \n",
       "min          0.000000            0.000000                0.000000   \n",
       "25%        255.000000           46.000000                0.410000   \n",
       "50%        255.000000          255.000000                1.000000   \n",
       "75%        255.000000          255.000000                1.000000   \n",
       "max        255.000000          255.000000                1.000000   \n",
       "\n",
       "       dst_host_diff_srv_rate  dst_host_same_src_port_rate  \\\n",
       "count           494021.000000                494021.000000   \n",
       "mean                 0.030906                     0.601935   \n",
       "std                  0.109259                     0.481309   \n",
       "min                  0.000000                     0.000000   \n",
       "25%                  0.000000                     0.000000   \n",
       "50%                  0.000000                     1.000000   \n",
       "75%                  0.040000                     1.000000   \n",
       "max                  1.000000                     1.000000   \n",
       "\n",
       "       dst_host_srv_diff_host_rate  dst_host_serror_rate  \\\n",
       "count                494021.000000         494021.000000   \n",
       "mean                      0.006684              0.176754   \n",
       "std                       0.042133              0.380593   \n",
       "min                       0.000000              0.000000   \n",
       "25%                       0.000000              0.000000   \n",
       "50%                       0.000000              0.000000   \n",
       "75%                       0.000000              0.000000   \n",
       "max                       1.000000              1.000000   \n",
       "\n",
       "       dst_host_srv_serror_rate  dst_host_rerror_rate  \\\n",
       "count             494021.000000         494021.000000   \n",
       "mean                   0.176443              0.058118   \n",
       "std                    0.380919              0.230590   \n",
       "min                    0.000000              0.000000   \n",
       "25%                    0.000000              0.000000   \n",
       "50%                    0.000000              0.000000   \n",
       "75%                    0.000000              0.000000   \n",
       "max                    1.000000              1.000000   \n",
       "\n",
       "       dst_host_srv_rerror_rate  \n",
       "count             494021.000000  \n",
       "mean                   0.057412  \n",
       "std                    0.230140  \n",
       "min                    0.000000  \n",
       "25%                    0.000000  \n",
       "50%                    0.000000  \n",
       "75%                    0.000000  \n",
       "max                    1.000000  \n",
       "\n",
       "[8 rows x 38 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# load data (10%)\n",
    "import pandas\n",
    "from time import time\n",
    "col_names = [\"duration\",\"protocol_type\",\"service\",\"flag\",\"src_bytes\",\n",
    "    \"dst_bytes\",\"land\",\"wrong_fragment\",\"urgent\",\"hot\",\"num_failed_logins\",\n",
    "    \"logged_in\",\"num_compromised\",\"root_shell\",\"su_attempted\",\"num_root\",\n",
    "    \"num_file_creations\",\"num_shells\",\"num_access_files\",\"num_outbound_cmds\",\n",
    "    \"is_host_login\",\"is_guest_login\",\"count\",\"srv_count\",\"serror_rate\",\n",
    "    \"srv_serror_rate\",\"rerror_rate\",\"srv_rerror_rate\",\"same_srv_rate\",\n",
    "    \"diff_srv_rate\",\"srv_diff_host_rate\",\"dst_host_count\",\"dst_host_srv_count\",\n",
    "    \"dst_host_same_srv_rate\",\"dst_host_diff_srv_rate\",\"dst_host_same_src_port_rate\",\n",
    "    \"dst_host_srv_diff_host_rate\",\"dst_host_serror_rate\",\"dst_host_srv_serror_rate\",\n",
    "    \"dst_host_rerror_rate\",\"dst_host_srv_rerror_rate\",\"label\"]\n",
    "kdd_data_10percent = pandas.read_csv(\"data/kddcup.data_10_percent\", header=None, names = col_names)\n",
    "kdd_data_10percent.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "# import modules\n",
    "from sklearn import svm\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "from sklearn.decomposition import PCA\n",
    "from pylab import *\n",
    "import struct\n",
    "import keras as ks\n",
    "import logging\n",
    "from keras.layers import Dense, Activation, Flatten, Convolution2D\n",
    "from keras.utils import np_utils\n",
    "from keras.models import model_from_json\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from skimage import io\n",
    "import numpy as np\n",
    "from PIL import Image \n",
    "from scipy import misc\n",
    "import os\n",
    "# ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 493021 * 42\n",
    "kdd_data = np.array(kdd_data_10percent)\n",
    "# 某几列 symbolic => 数值 [http://kdd.ics.uci.edu/databases/kddcup99/kddcup.names]\n",
    "# remove protocol_type, service, flag, label\n",
    "# 493021 * 38 features\n",
    "# Normalization: Feature Scaling\n",
    "# 处理 Label\n",
    "\n",
    "# PCA + classification\n",
    "# classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# features => 38\n",
    "num_features = [\n",
    "    \"duration\",\"src_bytes\",\n",
    "    \"dst_bytes\",\"land\",\"wrong_fragment\",\"urgent\",\"hot\",\"num_failed_logins\",\n",
    "    \"logged_in\",\"num_compromised\",\"root_shell\",\"su_attempted\",\"num_root\",\n",
    "    \"num_file_creations\",\"num_shells\",\"num_access_files\",\"num_outbound_cmds\",\n",
    "    \"is_host_login\",\"is_guest_login\",\"count\",\"srv_count\",\"serror_rate\",\n",
    "    \"srv_serror_rate\",\"rerror_rate\",\"srv_rerror_rate\",\"same_srv_rate\",\n",
    "    \"diff_srv_rate\",\"srv_diff_host_rate\",\"dst_host_count\",\"dst_host_srv_count\",\n",
    "    \"dst_host_same_srv_rate\",\"dst_host_diff_srv_rate\",\"dst_host_same_src_port_rate\",\n",
    "    \"dst_host_srv_diff_host_rate\",\"dst_host_serror_rate\",\"dst_host_srv_serror_rate\",\n",
    "    \"dst_host_rerror_rate\",\"dst_host_srv_rerror_rate\"\n",
    "]\n",
    "features = kdd_data_10percent[num_features].astype(float)\n",
    "np_features = np.array(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# labels\n",
    "labels = kdd_data_10percent['label'].copy()\n",
    "labels[labels!='normal.'] = 'attack.'\n",
    "labels.value_counts()\n",
    "np_labels = np.array(labels)\n",
    "np_labels[np_labels == 'normal.'] = 0\n",
    "np_labels[np_labels == 'attack.'] = 1\n",
    "np_labels = np.array(np_labels, dtype = np.float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Feature scaling\n",
    "for i in range(38):\n",
    "    d_min = min(np_features[:][i])\n",
    "    d_max = max(np_features[:][i])\n",
    "    np_features[:][i] -= d_min\n",
    "    np_features[:][i] /= d_max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sample_index = []\n",
    "n_step = int(494021 / 1000)\n",
    "for i in range(1000):\n",
    "    sample_index.append(i*n_step)\n",
    "sample_index = np.array(sample_index)\n",
    "np_1000_features = np_features[sample_index]\n",
    "np_1000_labels = np_labels[sample_index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# data for Testing\n",
    "np_feature_test = np_features[sample_index + 3]\n",
    "np_labels_test = np_labels[sample_index + 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
       "  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',\n",
       "  max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
       "  tol=0.001, verbose=False)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# SVM\n",
    "clf = svm.SVC()\n",
    "clf.fit(np_1000_features, np_1000_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.979\n"
     ]
    }
   ],
   "source": [
    "# SVM Testing\n",
    "res_svm = clf.predict(np_feature_test)\n",
    "print(sum(res_svm == np_labels_test)/1000.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.99928543, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.59526794, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39540422, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882,  0.99966333, -0.39496882,  0.99966333,  1.02495438,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39719394, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39513318, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.41016639, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39568143, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.3952473 , -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.41098352, -0.39496882, -0.39496882,\n",
       "       -0.39496882,  1.06756472,  1.06756472, -0.39496882, -0.60636399,\n",
       "       -0.39496882, -1.00048156, -0.39496882,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  1.04081276,  0.99966333,\n",
       "        0.99966333,  1.04081276,  0.99966333, -0.39496882,  0.88435481,\n",
       "        0.88435019, -0.39496882, -0.39496882, -0.44218469, -0.31374857,\n",
       "        0.95905873,  0.97068059,  0.70146738,  1.10121935,  0.97006274,\n",
       "        0.91942016,  0.93124288,  1.0924559 ,  1.10093484,  0.86572366,\n",
       "        0.91834908,  0.9279638 , -0.09358825,  1.10121935,  0.95827482,\n",
       "        0.97257205,  0.49194445,  1.10229409,  0.91443483,  0.89572513,\n",
       "        1.06228147,  0.91949487,  0.95603127,  0.44365946,  1.10795551,\n",
       "        0.97209784,  1.15357003,  1.09505904, -0.04360118,  0.87662546,\n",
       "        1.01376673,  1.03259278,  0.96743943,  0.46346059,  0.9110516 ,\n",
       "        0.89524399, -0.31697366,  1.01516738,  0.99617011,  0.0966126 ,\n",
       "       -0.39496882, -0.39498672, -0.39496882, -0.39627185, -0.39088205,\n",
       "       -0.39538139, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882,  1.00035442, -0.39496882,\n",
       "       -0.39496882, -0.39496919, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882,  1.00120861,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  1.04081276,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "       -0.39496882, -0.39496885, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39496882,  1.00316284,\n",
       "        1.13420854,  1.02445377,  0.99583596,  1.24399891,  1.08695171,\n",
       "        0.55643768,  1.01800294,  0.5512534 ,  0.96314488,  1.00025457,\n",
       "        0.74587095,  0.90971874,  0.91568855,  1.13462443,  0.99235158,\n",
       "        0.86162769,  1.06875236,  0.70002024,  1.08202483,  0.97811547,\n",
       "        1.03420876,  0.26221139,  1.16537235,  0.7408951 ,  0.99492156,\n",
       "       -0.39495842,  0.32270175,  0.99694767,  1.00058766,  1.09096671,\n",
       "        0.96531478,  1.02650253,  1.07646151,  0.58630177,  1.13333671,\n",
       "        0.99902491,  1.06998973,  0.81546773,  1.0228785 ,  0.48459325,\n",
       "        1.09212741,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        1.04081276,  1.04081276,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333, -0.39496882, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882, -0.39496882, -0.39496882,\n",
       "        1.00113438, -0.39496882, -0.39496886, -0.62873243, -0.43255038,\n",
       "       -0.39496882, -0.75726883, -1.03678509,  1.00061826, -1.03811025,\n",
       "       -0.39508263, -0.39496882, -0.39496882, -0.39686667, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882,  0.9855041 ,  1.04081276,\n",
       "        0.99966333,  0.99966333,  0.99966333,  1.04081276,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  1.04081276,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  1.04081276,  1.04081276,  0.99966333,  0.99966333,\n",
       "        1.04081276,  1.04081276,  1.04081276,  0.99966333,  0.99966333,\n",
       "        1.04081276,  1.02495438,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        1.04081276,  0.99966333,  0.99966333,  1.04081276,  0.99966333,\n",
       "        1.04081276,  0.99966333,  0.99966333,  1.04081276,  0.99966333,\n",
       "        1.04081276,  0.99964631,  0.99966333,  0.99966333,  1.02495438,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  1.01133283,\n",
       "        0.99966333,  1.02495438,  1.04081276,  1.04081276,  1.04081276,\n",
       "        0.99966333,  0.99966333,  1.02495438,  0.99966333,  0.99966333,\n",
       "        1.01133283,  0.99966333,  1.04081276,  0.99966333,  0.99966333,\n",
       "        1.04081276,  1.04081276,  0.99966333,  1.04081276,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  1.04081276,\n",
       "        0.99966333,  0.99966333,  1.04081276,  1.04081276,  0.99966333,\n",
       "        0.99966333,  0.99966333,  1.04081276,  0.99966333,  0.99966333,\n",
       "        1.04081276,  1.02495438,  0.99966333,  0.99966333,  1.02495438,\n",
       "        0.99966333,  0.99966333,  1.04081276,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  1.04081276,  1.04081276,\n",
       "        1.04081276,  0.99966333,  0.99966333,  0.99966333,  1.04081276,\n",
       "        1.04081276,  1.02495438,  1.04081276,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        1.04081276,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99964631,  0.99966333,  0.99966333,  0.99966333,  1.04081276,\n",
       "        1.01839273,  0.99966333,  0.99966333,  0.99966333,  1.04081276,\n",
       "        1.04081276,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        1.01133283,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  1.04081276,  1.02495438,  0.99966333,\n",
       "        0.99966333,  1.04081276,  1.02495438,  0.99966333,  1.04081276,\n",
       "        0.99966333,  0.99966333,  0.99966333,  1.04081276,  0.99964631,\n",
       "        0.99966333,  0.99966333,  0.99966333,  1.04081276,  0.99966333,\n",
       "        0.99966333,  1.04081276,  1.02495438,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        1.04081276,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  1.04081276,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  1.04081276,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333,  0.99966333,\n",
       "       -1.00180503, -0.39496882, -0.39657823, -0.39496882, -0.39614759,\n",
       "       -0.39496882, -0.39496882,  0.63121245, -0.81581796, -0.87573315,\n",
       "       -0.93936986, -0.39496882, -0.91137691,  0.80650979,  0.91449337,\n",
       "        0.61063335, -0.38472081,  0.11934562,  0.40189146,  0.92040736,\n",
       "        0.51241802,  0.42078884,  0.21391922,  0.86768008,  0.66733273,\n",
       "        0.80596208,  0.50436268, -0.31997242,  1.05881405, -0.2794137 ,\n",
       "        1.10729838, -0.22180548,  0.24206328,  1.04148171,  1.01471864,\n",
       "        0.72407209,  1.15277096,  0.83716928,  0.95951702,  0.97676353,\n",
       "        1.0943738 ,  0.36235473,  0.88834615,  0.90997141,  0.87269224,\n",
       "        0.99682213,  0.37625941,  0.35724525,  0.78112894,  0.73891752,\n",
       "        0.31728351,  0.97169329,  0.38985362,  0.86944838,  1.00017122,\n",
       "        0.99966333,  0.99966333, -0.99491916, -0.39496882,  0.06706795,\n",
       "        1.12389938,  0.60755936,  0.70773402,  0.64833695,  1.00037535,\n",
       "        0.94398479,  0.7467487 ,  1.0489022 ,  1.07768674,  1.09265914,\n",
       "        1.0134513 ,  0.90809079,  0.91057745,  0.34035648,  0.87624494,\n",
       "        0.10408412,  0.25750157, -0.3759893 ,  0.05264944, -0.31698828,\n",
       "        0.7660924 ,  0.77444961,  1.02167027,  0.49827795,  1.11109941,\n",
       "        0.3016605 ,  0.05539151,  0.98019582,  1.0828106 ,  0.27764173,\n",
       "        1.00271908,  1.14798691,  0.12771868,  0.41183783,  1.10349649,\n",
       "        0.79340543,  0.90015878,  0.93476144,  1.00011477,  0.99964543,\n",
       "        1.06276998,  1.08517653,  0.99694176,  1.13333621,  1.0216876 ,\n",
       "        1.0210582 ,  1.01314537,  0.88785169, -0.39496882,  0.99961324,\n",
       "        1.00044654,  0.85021463,  1.0059968 ,  1.04232184,  0.98863558,\n",
       "        0.99955417,  0.98448155,  1.00046408,  0.36522505, -0.07153874,\n",
       "        1.03113517,  0.84342579,  0.98863558,  1.07113006,  1.00006901,\n",
       "        0.74999436,  0.99974981,  1.00046408,  0.8443336 ,  0.98121712,\n",
       "        0.99955417,  1.00044654,  1.00046408,  1.0706727 ,  0.81555374,\n",
       "        0.84342579,  1.00046408,  0.24167602,  1.03113517, -0.35348054,\n",
       "        1.013239  ,  1.07113006,  0.89929038,  0.44811575,  1.00006901,\n",
       "        1.03531685,  1.00006677,  0.64276836,  0.99974981,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987,  0.99992987,  0.99992987,  0.99992987,  0.99992987,\n",
       "        0.99992987, -1.02379803, -0.41782179, -0.39496882, -1.0001569 ,\n",
       "       -1.06635936, -0.39496882, -1.02438374, -0.90158217, -0.39496882,\n",
       "       -0.9997728 , -0.39496882, -0.39496882, -0.39496882, -0.40205708,\n",
       "       -1.00719853, -0.39496882, -0.97073821, -0.39496882, -0.39792031,\n",
       "       -0.39496882, -0.02935486,  1.06058331,  0.97276283,  0.891917  ,\n",
       "        0.92757679,  0.85771386,  0.60539146,  0.29859582,  0.84161462,\n",
       "        0.69038249,  1.00008757,  0.63662569,  0.98307242,  1.03061834,\n",
       "        1.06462308,  1.05098729,  0.80868888,  0.995151  ,  1.0313739 ,\n",
       "        0.902366  ,  0.48286045,  0.85282869,  0.05916401, -0.37163437,\n",
       "       -0.37287165,  0.97202172,  0.97991011,  0.79397819,  0.95573048,\n",
       "        1.08458593,  0.93080637,  0.82132314,  0.80533985,  0.95692095,\n",
       "        0.89639817,  0.88140488,  0.72636019,  0.61224522,  1.04715493,\n",
       "        0.94959624,  0.98868922,  0.12297098, -0.39496882, -0.99989712,\n",
       "       -0.92166196, -0.39496882, -0.97073382, -0.39496882, -0.39496882,\n",
       "       -0.39496882, -0.39496882, -0.39496882,  0.99768634, -0.39496882,\n",
       "       -0.39496882, -0.39496882,  0.99966333,  0.99966333,  0.99966333,\n",
       "        0.99966333,  0.99966333,  0.99966333,  0.99966333, -0.9593588 ,\n",
       "       -0.39498332, -0.39496882, -0.39496882, -0.39496882, -0.39496882])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# distance from sample x and hyperplane\n",
    "clf.decision_function(np_feature_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "labels_for_nn = []\n",
    "np_labels_test_nn = []\n",
    "for i in range(len(np_1000_labels)):\n",
    "    tmp_label = [0] * 2\n",
    "    tmp_label[int(np_1000_labels[i])] = 1\n",
    "    labels_for_nn.append(tmp_label)\n",
    "    \n",
    "    tmp_label = [0] * 2\n",
    "    tmp_label[int(np_labels_test[i])] = 1\n",
    "    np_labels_test_nn.append(tmp_label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /Users/lyq/Anaconda/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py:2755: calling reduce_sum (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "keep_dims is deprecated, use keepdims instead\n",
      "WARNING:tensorflow:From /Users/lyq/Anaconda/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py:1290: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "keep_dims is deprecated, use keepdims instead\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/lyq/Anaconda/anaconda2/lib/python2.7/site-packages/keras/models.py:848: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.\n",
      "  warnings.warn('The `nb_epoch` argument in `fit` '\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 1000 samples, validate on 1000 samples\n",
      "Epoch 1/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0943 - acc: 0.9880 - val_loss: 0.1030 - val_acc: 0.9870\n",
      "Epoch 2/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0891 - acc: 0.9900 - val_loss: 0.0998 - val_acc: 0.9880\n",
      "Epoch 3/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0824 - acc: 0.9910 - val_loss: 0.0860 - val_acc: 0.9900\n",
      "Epoch 4/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0741 - acc: 0.9930 - val_loss: 0.0863 - val_acc: 0.9900\n",
      "Epoch 5/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0803 - acc: 0.9910 - val_loss: 0.0852 - val_acc: 0.9890\n",
      "Epoch 6/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0738 - acc: 0.9920 - val_loss: 0.0909 - val_acc: 0.9910\n",
      "Epoch 7/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0784 - acc: 0.9930 - val_loss: 0.0881 - val_acc: 0.9910\n",
      "Epoch 8/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0811 - acc: 0.9920 - val_loss: 0.0800 - val_acc: 0.9900\n",
      "Epoch 9/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0698 - acc: 0.9950 - val_loss: 0.0833 - val_acc: 0.9900\n",
      "Epoch 10/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0814 - acc: 0.9900 - val_loss: 0.0903 - val_acc: 0.9910\n",
      "Epoch 11/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0833 - acc: 0.9920 - val_loss: 0.0785 - val_acc: 0.9910\n",
      "Epoch 12/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0778 - acc: 0.9910 - val_loss: 0.0914 - val_acc: 0.9910\n",
      "Epoch 13/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0764 - acc: 0.9930 - val_loss: 0.0795 - val_acc: 0.9880\n",
      "Epoch 14/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0780 - acc: 0.9910 - val_loss: 0.0760 - val_acc: 0.9910\n",
      "Epoch 15/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0705 - acc: 0.9940 - val_loss: 0.0767 - val_acc: 0.9910\n",
      "Epoch 16/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0744 - acc: 0.9920 - val_loss: 0.0778 - val_acc: 0.9920\n",
      "Epoch 17/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0721 - acc: 0.9930 - val_loss: 0.0848 - val_acc: 0.9910\n",
      "Epoch 18/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0740 - acc: 0.9950 - val_loss: 0.0765 - val_acc: 0.9920\n",
      "Epoch 19/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0729 - acc: 0.9920 - val_loss: 0.0767 - val_acc: 0.9920\n",
      "Epoch 20/20\n",
      "1000/1000 [==============================] - 0s - loss: 0.0724 - acc: 0.9920 - val_loss: 0.0737 - val_acc: 0.9920\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x122689c10>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = ks.models.Sequential()\n",
    "model.add(Dense(128, input_dim=38))\n",
    "model.add(Activation('relu'))\n",
    "model.add(Dense(64))\n",
    "model.add(Activation('relu'))\n",
    "model.add(Dense(2))\n",
    "model.add(Activation('softmax'))\n",
    "\n",
    "model.compile(loss='categorical_crossentropy',optimizer='adadelta',metrics=['accuracy'])\n",
    "model.fit(x=np_1000_features,y=labels_for_nn,batch_size=100,nb_epoch=20,verbose=1,validation_data=(np_feature_test,np_labels_test_nn))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.55775112,  0.44224885],\n",
       "       [ 1.        ,  0.        ],\n",
       "       [ 1.        ,  0.        ],\n",
       "       ..., \n",
       "       [ 1.        ,  0.        ],\n",
       "       [ 1.        ,  0.        ],\n",
       "       [ 1.        ,  0.        ]], dtype=float32)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(np_feature_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# PCA\n",
    "pca = PCA(n_components=0.9999)\n",
    "pca.fit(np_1000_features)\n",
    "pca_data = pca.transform(np_1000_features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1000, 38)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_1000_features.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1000, 6)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PCA(copy=True, iterated_power='auto', n_components=0.9999, random_state=None,\n",
       "  svd_solver='auto', tol=0.0, whiten=False)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ecr_i          281400\n",
       "private        110893\n",
       "http            64293\n",
       "smtp             9723\n",
       "other            7237\n",
       "domain_u         5863\n",
       "ftp_data         4721\n",
       "eco_i            1642\n",
       "ftp               798\n",
       "finger            670\n",
       "urp_i             538\n",
       "telnet            513\n",
       "ntp_u             380\n",
       "auth              328\n",
       "pop_3             202\n",
       "time              157\n",
       "csnet_ns          126\n",
       "remote_job        120\n",
       "imap4             117\n",
       "gopher            117\n",
       "domain            116\n",
       "discard           116\n",
       "systat            115\n",
       "iso_tsap          115\n",
       "echo              112\n",
       "shell             112\n",
       "rje               111\n",
       "whois             110\n",
       "sql_net           110\n",
       "printer           109\n",
       "                ...  \n",
       "uucp              106\n",
       "vmnet             106\n",
       "uucp_path         106\n",
       "klogin            106\n",
       "supdup            105\n",
       "ssh               105\n",
       "nnsp              105\n",
       "login             104\n",
       "hostnames         104\n",
       "daytime           103\n",
       "efs               103\n",
       "netbios_ns        102\n",
       "link              102\n",
       "ldap              101\n",
       "pop_2             101\n",
       "netbios_dgm        99\n",
       "exec               99\n",
       "http_443           99\n",
       "kshell             98\n",
       "name               98\n",
       "ctf                97\n",
       "netstat            95\n",
       "Z39_50             92\n",
       "IRC                43\n",
       "urh_i              14\n",
       "X11                11\n",
       "tim_i               7\n",
       "pm_dump             1\n",
       "red_i               1\n",
       "tftp_u              1\n",
       "Name: service, Length: 66, dtype: int64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kdd_data_10percent['service'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
